import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class Ex04_SMTP {
    
    public static void sendMail(String recipient, String subject, String content) {
    // config
        // Sender's email ID needs to be mentioned
        String from = "netexample@yandex.ru";

        // Get system properties
        Properties properties = System.getProperties();
        // Setup mail server
        properties.setProperty("mail.smtps.host", "smtp.yandex.ru");
        properties.setProperty("mail.smtps.port", "465");
        properties.setProperty("mail.smtps.auth", "true");
        properties.setProperty("mail.user", "netexample");
        properties.setProperty("mail.password", "examplenet");
    //properties.put("mail.debug", "true");

        // Get the default Session object.
        Session session = Session.getDefaultInstance(properties);

        try {
            MimeMessage message = new MimeMessage(session);

            // Set From: header field of the header.
            message.setFrom(new InternetAddress(from));

            // Set To: header field of the header.
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(recipient));

            message.setSubject(subject);
            message.setText(content);

            Transport trnsport;
            trnsport = session.getTransport("smtps");
            trnsport.connect(null, properties.getProperty("mail.password"));
            message.saveChanges();
            trnsport.sendMessage(message, message.getAllRecipients());
            trnsport.close();
            System.out.println("Sent message successfully....");
        } catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }

 	public static void main(String[] args) {

            String to = "ddzuba@yandex.ru";
            String subject = "Congratulation";
            String content = "Stop sending mail to yourself!";
            sendMail(to, subject, content);

	}
        
        
}
